﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.Traffic;

/// <summary>
/// 品牌维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改品牌维护在一个页面。
/// </summary>
public partial class Traffic_Trucks_List : PageBase
{
    TrafficManager manager = new TrafficManager();
    Jumpton.Persistent.ObjectTable ot;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetChains();
            GetTrucksBind();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetTrucksBind()
    {
        ot = manager.GetTrucks(MyStateManager.DomainID, ddlChains.SelectedValue,ddlValid.SelectedValue,txt_code.Text.Trim());

        GridPager1.DataSource = ot;
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;
        
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    
    }

    #region 绑定经销商
    public void GetChains()
    {
        ddlChains.DataTextField = "DESCRIPTION";
        ddlChains.DataValueField = "ID";
        ot = new ChainManager().GetCourseTable(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=TRUE";
            ddlChains.DataSource = ot.BillDataTable.DefaultView;
            ddlChains.DataBind();

            
        }
        ddlChains.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

   

    /// <summary>
    /// 根据条件数据查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        GetTrucksBind();
    }

    /// <summary>
    /// 新增方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
     
    }

   

    /// <summary>
    /// 保存数据方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            TRUCKS truck = null;

            TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox txt_DESCRIPTION = DetailsView1.FindControl("txt_DESCRIPTION") as TextBox;
            DropDownList ddlChain = DetailsView1.FindControl("ddlChain") as DropDownList;
            CheckBox chkValid=DetailsView1.FindControl("chkValid") as CheckBox;
            TextBox txtweight=DetailsView1.FindControl("txtWeight") as TextBox;
            TextBox txtVolume=DetailsView1.FindControl("txtVolume") as TextBox;
            TextBox txtMaxVolume = DetailsView1.FindControl("txtMaxVolume") as TextBox;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                truck = new TRUCKS();
                truck.CREATETIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
                truck.CODE = ctlcode.Text.Trim();
                truck.CODE = truck.CODE.Replace(" ", "");
                //判断是否存在此代码
                if (manager.ExistsTrucks(truck.CODE))
                {
                    ShowMessage("已存在此车牌");
                    return;
                }
                
            }
            else
            {
                truck = manager.LoadTruck(ctlcode.Text);

                
            }
            truck.DESCRIPTION = txt_DESCRIPTION.Text.Trim();
            truck.LAST_UPDATER = MyStateManager.UserID.ToString();
            truck.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
            truck.WEIGHT =decimal.Parse(txtweight.Text.Trim());
            truck.VOLUME = decimal.Parse(txtVolume.Text.Trim());
            truck.MAXVOLUME = decimal.Parse(txtMaxVolume.Text.Trim());
            truck.ACTIVE = chkValid.Checked;
            truck.DOMAINID = MyStateManager.DomainID;
            truck.CHAINSID = int.Parse(ddlChain.SelectedValue);
            string LogContent = "";
            LogContent += "增加或修改新的车辆，" + ctlcode.Text + "的信息是：";


            LogContent += "车牌号：" + truck.CODE + "，";
            LogContent += "车辆描述：" + truck.DESCRIPTION+ "，";
            LogContent += "容量：" + truck.VOLUME + "，";
            LogContent += "最大容量：" + truck.MAXVOLUME + "，";
            LogContent += "重量：" + truck.WEIGHT + "，";
            LogContent += "有效：" + truck.ACTIVE;

            if (manager.SaveTruck(truck))
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                 Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                 this.MyStateManager.UserID, LogContent);
                if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                {
                    ShowMessage("保存成功");
                    GetTrucksBind();
                   
                }
                else
                {
                    ShowMessage("保存成功", Request.RawUrl.ToString());
                   // Response.Redirect(Request.RawUrl.ToString());
                }
            }
           
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                   Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                   this.MyStateManager.UserID, "车辆维护 - 异常:" + ex.Message);
        }
    }

   

    /// <summary>
    /// 编辑时数据绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlChain = DetailsView1.FindControl("ddlChain") as DropDownList;
        if (ddlChain != null)
        {
            ddlChain.DataTextField = "DESCRIPTION";
            ddlChain.DataValueField = "ID";
            ot = new ChainManager().GetCourseTable(MyStateManager.DomainID);
            if (ot != null)
            {
                ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=TRUE";
                ddlChain.DataSource = ot.BillDataTable.DefaultView;
                ddlChain.DataBind();
            }
        }
        if (DetailsView1.DataItem == null)
        {
            CheckBox chkValid = DetailsView1.FindControl("chkValid") as CheckBox;
            chkValid.Checked = true;
        }
        else
        {
            TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
            ctlcode.Enabled = false;
            TRUCKS t = DetailsView1.DataItem as TRUCKS;
            ddlChain.SelectedValue = t.CHAINSID.ToString();

        }

       
    }

    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
       
    }
    //protected void btnAdd0_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("/Server/ExportExcel/ExportExcel.aspx?Table=" + "Brands" + "&Category=" + ddlCategroy.SelectedValue + "&Manu=" + ddlManu.SelectedValue + "&Valid=" + ddlValid.SelectedValue);
    //}

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetTrucksBind();
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string value=e.Row.Cells[5].Text;
            if(value!=null&&value!="")
            {
                e.Row.Cells[5].Text = ddlChains.Items.FindByValue(value).Text;
                if (e.Row.Cells[5].Text.Trim() == "")
                    e.Row.Cells[5].Text = value;
            }

        }
    }
}
